package com.realgodo.site.web.user;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.realgodo.site.common.Constant;
import com.realgodo.site.entity.dic.Face;
import com.realgodo.site.entity.security.User;
import com.realgodo.site.entity.space.Space;
import com.realgodo.site.entity.vocation.Vocation;
import com.realgodo.site.service.UserManager;
import com.realgodo.site.service.VocationManager;
import com.realgodo.site.service.dic.FaceManager;
import com.realgodo.site.service.relation.FriendManager;
import com.realgodo.site.service.space.SpaceManager;
import com.realgodo.site.web.ControllerSupport;
import com.realgodo.site.web.interceptor.UserAware;

/**
 * @author bijiqiang E-mail: bijiqiang@gmail.com
 * @version 创建时间：2009-12-13下午12:32:51
 * @since jdk1.5
 * @version 0.1
 */
public class UserDefaultController extends ControllerSupport implements
		UserAware {

	private static final long serialVersionUID = -7835995715871697201L;
	protected Logger logger = LoggerFactory.getLogger(getClass());
	protected Long id;

	protected static final int PAGE_SIZE = 10;
	/** 热门评论量下线 */
	protected static final Integer HOT_LIMIT = 1;
	@Autowired
	protected SessionFactory sessionFactory;
	@Autowired
	protected SpaceManager spaceManager;
	@Autowired
	private FaceManager faceManager;
	@Autowired
	protected UserManager userManager;
	@Autowired
	protected FriendManager friendManager;
	@Autowired
	private VocationManager vocationManager;

	private User user;
	/** 用户本身的space */
	private Space space;

	/** 全部表情列表 */
	private List<Face> faceList;

	private List<Face> commenFaceList;

	private List<Face> planeFaceList;

	private List<Face> dimensionalFaceList;

	/** 当前登录者的space */
	public Space getSpace() {
		if (null != getUser()) {
			space = (Space) getSession().getAttribute(Constant.LOGIN_SPACE);
		} else {
			space = null;
		}
		return space;
	}

	public void setLoginSpace(Space space) {
		getSession().setAttribute(Constant.LOGIN_SPACE, space);
		this.space = space;
	}

	public void setSpace(Space space) {
		this.space = space;
	}

	/** 当前登录用户 */
	public User getUser() {
		Long userid = (Long) getSession().getAttribute(Constant.LOGIN_USER_ID);
		if (user == null) {
			user = userManager.getUserForDisplay(userid);
		}
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	private void spiltFaceList() {
		faceList = getFaceList();
		commenFaceList = new ArrayList<Face>();
		planeFaceList = new ArrayList<Face>();
		dimensionalFaceList = new ArrayList<Face>();
		for (Face face : faceList) {
			if (face.getType() == 1) {
				commenFaceList.add(face);
			} else if (face.getType() == 2) {
				planeFaceList.add(face);
			} else {
				dimensionalFaceList.add(face);
			}
		}
	}

	// -----------get/set-----//
	public List<Face> getFaceList() {
		if (faceList == null) {
			faceList = faceManager.getAll();
		}
		return faceList;
	}

	public List<Face> getCommenFaceList() {
		if (commenFaceList == null) {
			spiltFaceList();
		}
		return commenFaceList;
	}

	public List<Face> getPlaneFaceList() {
		if (planeFaceList == null) {
			spiltFaceList();
		}
		return planeFaceList;
	}

	public List<Face> getDimensionalFaceList() {
		if (dimensionalFaceList == null) {
			spiltFaceList();
		}
		return dimensionalFaceList;
	}

	/**
	 * 获取职业
	 * 
	 * @return
	 */
	public List<Vocation> getVocations() {
		return this.vocationManager.getMyVocation(getUser());
	}

}
